package com.htjc.uep.center.service.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.htjc.uep.center.service.mapper.ExtendedMapper;
import com.htjc.uep.center.service.service.ICenterService;

import com.htjc.uep.center.commons.utils.JsonUtil;
import com.htjc.uep.center.commons.utils.SignUtil;
import com.htjc.uep.center.commons.utils.StringUtil;
import com.htjc.uep.center.model.enums.ErrorStatus;
import com.htjc.uep.center.model.service.request.QueryPoolRequest;
import com.htjc.uep.center.model.service.request.DeductionPointsRequest;
import com.htjc.uep.center.model.service.request.QueryAccountCodeRequest;

@RequestMapping("/ac/business")
@RestController
public class BusinessController {
private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ICenterService centerService;

	@Autowired
	private ExtendedMapper mapper;
	
	@RequestMapping(value = {"queryPoolApply"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryPoolApply(@RequestBody String str) {
		try {
			log.info("积分中心-积分池查询业务申请接口-入参={}", str);
			QueryPoolRequest queryPoolRequest = JsonUtil.json2Bean(str, QueryPoolRequest.class);
			//判断入参是否为空
			if (StringUtil.isEmpty(queryPoolRequest.getMechanismCode(), queryPoolRequest.getAccountCode(), queryPoolRequest.getSign())) {
				log.info("====>>>>积分池查询业务<<<<====积分池查询业务失败，入参存在空值-入参={}", queryPoolRequest.toString());
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			SignUtil signUtil = SignUtil.getInstance();
			String mechanismCodeSign = queryPoolRequest.getMechanismCode();
			String accountCodeSign = queryPoolRequest.getAccountCode();
			
			String param = mechanismCodeSign + accountCodeSign;
			if (!signUtil.verifySign(queryPoolRequest.getSign(), param)) {
				log.info("====>>>>积分池查询业务<<<<====验签失败");
				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
			}
			log.info("积分中心-积分池查询业务申请接口-申请类型=积分池查询");
			queryPoolRequest = signUtil.getDEBean(queryPoolRequest, "1");
			
			String response = centerService.queryPoolApply(queryPoolRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}		
	}
	
	/**
	 * 测试环境，无验签，无加密
	 * @param str
	 * @return
	 */
	@RequestMapping(value = {"queryPoolApplyNoAES"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryPoolApplyNoAES(@RequestBody String str) {
		try {
			log.info("积分中心-积分池查询业务申请接口-入参={}", str);
			QueryPoolRequest queryPoolRequest = JsonUtil.json2Bean(str, QueryPoolRequest.class);
			//判断入参是否为空
			if (StringUtil.isEmpty(queryPoolRequest.getMechanismCode(), queryPoolRequest.getAccountCode(), queryPoolRequest.getSign())) {
				log.info("====>>>>积分池查询业务<<<<====积分池查询业务失败，入参存在空值-入参={}", queryPoolRequest.toString());
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
//			SignUtil signUtil = SignUtil.getInstance();
//			String mechanismCodeSign = queryPoolRequest.getMechanismCode();
//			String accountCodeSign = queryPoolRequest.getAccountCode();
			
//			String param = mechanismCodeSign + accountCodeSign;
//			if (!signUtil.verifySign(queryPoolRequest.getSign(), param)) {
//				log.info("====>>>>积分池查询业务<<<<====验签失败");
//				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
//			}
//			log.info("积分中心-积分池查询业务申请接口-申请类型=积分池查询");
//			queryPoolRequest = signUtil.getDEBean(queryPoolRequest, "1");
			
			String response = centerService.queryPoolApply(queryPoolRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}		
	}
	
	@RequestMapping(value = {"deductionPointApply"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String deductionPointApply(@RequestBody String str) {
		try {
			log.info("积分中心-积分池扣减业务申请接口-入参={}", str);
			DeductionPointsRequest businessApplyRequest = JsonUtil.json2Bean(str, DeductionPointsRequest.class);
			
			String dealIdSign = businessApplyRequest.getDealId();
			String mechanismCodeSign = businessApplyRequest.getMechanismCode();
			String accountCodeSign = businessApplyRequest.getAccountCode();
			String acctDateSign = businessApplyRequest.getAcctDate();
			String userIdSign = businessApplyRequest.getUserId();
			String phoneSign = businessApplyRequest.getPhone();
			String scoreSign = String.valueOf(businessApplyRequest.getScore());
			String bpTypeSign = businessApplyRequest.getBpType();
			String sendTimeSign = businessApplyRequest.getSendTime();
			String channelSign = businessApplyRequest.getChannel();
			
			//判断入参是否为空
			if (StringUtil.isEmpty(dealIdSign, mechanismCodeSign, accountCodeSign, acctDateSign, userIdSign, phoneSign, scoreSign, bpTypeSign, sendTimeSign, channelSign)) {
				log.info("====>>>>积分扣减业务<<<<====积分扣减业务失败，入参存在空值-入参={}", str);
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			
			String signParam = dealIdSign + accountCodeSign + mechanismCodeSign + acctDateSign + userIdSign + phoneSign
					 + scoreSign + bpTypeSign + sendTimeSign + channelSign;
			//验签
			SignUtil signUtil = SignUtil.getInstance();
			
			if (!signUtil.verifySign(businessApplyRequest.getSign(), signParam)) {
				log.info("====>>>>积分扣减业务<<<<====验签失败");
				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
			}
			//字段解密-0 加密 1解密
			businessApplyRequest = signUtil.getDEBean(businessApplyRequest, "1");
			
			log.info("积分中心-积分池扣减业务申请接口-积分池扣减");
			String response = centerService.deductionPointsApply(businessApplyRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}		
	}
	
	/**
	 * 河南积分抽奖用，无验签
	 * @param str
	 * @return
	 */
	@RequestMapping(value = {"dedPointApplyNoSign"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String dedPointApplyNoSign(@RequestBody String str) {
		try {
			log.info("积分中心-积分池扣减业务申请接口-入参={}", str);
			DeductionPointsRequest businessApplyRequest = JsonUtil.json2Bean(str, DeductionPointsRequest.class);
			
			String dealIdSign = businessApplyRequest.getDealId();
			String mechanismCodeSign = businessApplyRequest.getMechanismCode();
			String accountCodeSign = businessApplyRequest.getAccountCode();
			String acctDateSign = businessApplyRequest.getAcctDate();
			String userIdSign = businessApplyRequest.getUserId();
			String phoneSign = businessApplyRequest.getPhone();
			String scoreSign = String.valueOf(businessApplyRequest.getScore());
			String bpTypeSign = businessApplyRequest.getBpType();
			String sendTimeSign = businessApplyRequest.getSendTime();
			String channelSign = businessApplyRequest.getChannel();
			
			//判断入参是否为空
			if (StringUtil.isEmpty(dealIdSign, mechanismCodeSign, accountCodeSign, acctDateSign, userIdSign, phoneSign, scoreSign, bpTypeSign, sendTimeSign, channelSign)) {
				log.info("====>>>>积分扣减业务<<<<====积分扣减业务失败，入参存在空值-入参={}", str);
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			
//			String signParam = dealIdSign + accountCodeSign + mechanismCodeSign + acctDateSign + userIdSign + phoneSign
//					 + scoreSign + bpTypeSign + sendTimeSign + channelSign;
			//验签
			SignUtil signUtil = SignUtil.getInstance();
			
//			if (!signUtil.verifySign(businessApplyRequest.getSign(), signParam)) {
//				log.info("====>>>>积分扣减业务<<<<====验签失败");
//				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
//			}
			//字段解密-0 加密 1解密
			businessApplyRequest = signUtil.getDEBean(businessApplyRequest, "1");
			
			log.info("积分中心-积分池扣减业务申请接口-积分池扣减");
			String response = centerService.deductionPointsApply(businessApplyRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}	
	}
	
	/**
	 * 测试环境，无验签, 无加密
	 * @param str
	 * @return
	 */
	@RequestMapping(value = {"dedPointApplyNoAES"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String dedPointApplyNoAES(@RequestBody String str) {
		try {
			log.info("积分中心-积分池扣减业务申请接口-入参={}", str);
			DeductionPointsRequest businessApplyRequest = JsonUtil.json2Bean(str, DeductionPointsRequest.class);
			
			String dealIdSign = businessApplyRequest.getDealId();
			String mechanismCodeSign = businessApplyRequest.getMechanismCode();
			String accountCodeSign = businessApplyRequest.getAccountCode();
			String acctDateSign = businessApplyRequest.getAcctDate();
			String userIdSign = businessApplyRequest.getUserId();
			String phoneSign = businessApplyRequest.getPhone();
			String scoreSign = String.valueOf(businessApplyRequest.getScore());
			String bpTypeSign = businessApplyRequest.getBpType();
			String sendTimeSign = businessApplyRequest.getSendTime();
			String channelSign = businessApplyRequest.getChannel();
			
			//判断入参是否为空
			if (StringUtil.isEmpty(dealIdSign, mechanismCodeSign, accountCodeSign, acctDateSign, userIdSign, phoneSign, scoreSign, bpTypeSign, sendTimeSign, channelSign)) {
				log.info("====>>>>积分扣减业务<<<<====积分扣减业务失败，入参存在空值-入参={}", str);
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			
//			String signParam = dealIdSign + accountCodeSign + mechanismCodeSign + acctDateSign + userIdSign + phoneSign
//					 + scoreSign + bpTypeSign + sendTimeSign + channelSign;
			//验签
//			SignUtil signUtil = SignUtil.getInstance();
			
//			if (!signUtil.verifySign(businessApplyRequest.getSign(), signParam)) {
//				log.info("====>>>>积分扣减业务<<<<====验签失败");
//				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
//			}
//			//字段解密-0 加密 1解密
//			businessApplyRequest = signUtil.getDEBean(businessApplyRequest, "1");
			
			log.info("积分中心-积分池扣减业务申请接口-积分池扣减");
			String response = centerService.deductionPointsApply(businessApplyRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}	
	}
	
	@RequestMapping(value = {"queryAccountCode"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryAccountCode(@RequestBody String str) {
		try {
			mapper.insertLog(str, "01", "结算编码校验接口");
		} catch (Exception e) {
			log.error("");
		}
		
		try {
			log.info("积分中心-结算编码校验-入参={}", str);
			QueryAccountCodeRequest accountRequest = JsonUtil.json2Bean(str, QueryAccountCodeRequest.class);
			if (StringUtil.isEmpty(accountRequest.getAccountCode(), accountRequest.getMechanismCode(), accountRequest.getRepertoryId(), accountRequest.getSign())) {
				log.info("积分中心-结算编码校验-参数为空");
				return JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			SignUtil signUtil = SignUtil.getInstance();
			
			String mechanismCodeSign = accountRequest.getMechanismCode();
			String accountCodeSign = accountRequest.getAccountCode();
			
			String param = mechanismCodeSign + accountCodeSign;
			if (!signUtil.verifySign(accountRequest.getSign(), param)) {
				log.info("====>>>>结算编码校验<<<<====验签失败");
				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
			}
			accountRequest = signUtil.getDEBean(accountRequest, "1");
			
			String response = centerService.queryAccountCode(accountRequest);
			log.info("积分中心-结算编码校验完成-出参={}", response);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}
	}
}